package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 *
 * @author axi
 *
 */
public class CnTvBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.cntv.cn/thread[-[0-9]+]+.html",
				"http://bbs.cntv.cn/viewthread.php[?]tid=[0-9]+",
				"http://bbs.cntv.cn/viewthread.php\\?extra=page%3D1%26amp%3Bfilter%3D0%26amp%3Borderby%3Ddateline%26amp%3Bascdesc%3DDESC&tid=[0-9]*",
				"http://bbs.cntv.cn/viewthread.php\\?tid=[0-9]*&extra=page%3D1",
				"http://bbs.cntv.cn/viewthread.php[?]extra=page%3D1&tid=[0-9]+",
				"http://bbs.cntv.cn/viewthread.php[?]extra=page%3D1&page=1&tid=[0-9]+"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta bbs = new NewsMeta();
		String title = "";
		String content = "";
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;


		/**
		 * 标题
		 */
		title = doc.select("title").text().replaceAll("\\s*-\\s*.*", "").replace("复兴论坛", "");
		/**
		 * 查看，评论
		 */
		Elements select = doc.select("h1");
		if(select.size() > 0){
			String text = select.text();

			Pattern reg = Pattern.compile("[(][0-9]+/[0-9]+[)]");
			Matcher matcher = reg.matcher(text);
			if (matcher.find()) {
				String num = matcher.group();
				if(num != null){
					num = num.replace("(", "").replace(")", "");
					String[] split = num.split("/");
					if(split.length > 0){
						String click = split[1];
						try {
							clickNum = Integer.parseInt(click);
						} catch (NumberFormatException e) {
							clickNum = 0;
						}
						String comment = split[0];
						try {
							commentNum = Integer.parseInt(comment);
						} catch (NumberFormatException e) {
							commentNum = 0;
						}
						bbs.setUpdateUrl(url);
					}
				}
			}

		}
		/**
		 * 时间
		 */
		String author = doc.select("div.rigthInfo a[href*=space.php?]").text();
		if (StringUtils.isNullOrEmpty(author)){
			author = doc.select("td.postauthor a[href*=space.php?]").text();
		}


		String dateStr = doc.select("div.postinfos").text();
		date = DateUtils.matchDate(dateStr);
		if (date==null){
			dateStr = doc.select("div.rigthInfo p:contains(发布时间)").text();
			date = DateUtils.matchDate(dateStr);
			if(date==null){
				date=DateUtils.matchDate(doc.select("td.postauthor dl dd").text());
			}
		}

		/**
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc.select("div#firstpost"));
		if(StringUtils.isBlank(title)){
		}
		bbs.setUrl(url);
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(author);
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setType(4);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta != null){
			String url = meta.getUpdateUrl();
			if(!StringUtils.isBlank(url)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(url);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	/**
	 * @param args
	 */
	

	
}
